package com.example.demo.controller;

import com.example.demo.entity.bean.dataSource1.MemberBean;
import com.example.demo.entity.dataSource1.MemberEntity;
import com.example.demo.service.dataSource1.impl.MemberService;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Wen Gao on 2018/7/11.
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    private static final Logger logger = LogManager.getLogger(MemberController.class);

    @Autowired
    MemberService memberService;

    @ApiOperation(value = "根据memberUid获取member", notes = "不会返回password", response = MemberEntity.class)
    @ApiImplicitParam(name = "memberUid", value = "member唯一uid", required = true, dataType = "String")
    @RequestMapping(value = "/{memberUid}", method = RequestMethod.GET)
    @JsonView(MemberBean.WithoutPasswordView.class)
//    @JsonView(MemberEntity.WithPasswordView.class)
    public ResponseEntity<MemberBean> getMember(@PathVariable(name = "memberUid") long memberUid) {
        MemberBean memberBean = memberService.getMemberByMemberUid(memberUid);
        return ResponseEntity.ok(memberBean);
    }

    @ApiOperation(value = "添加一个member", notes = "notes")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity addMember(@ApiParam @ModelAttribute MemberBean memberBean) {
        logger.info(memberBean.toJson());
        if (memberBean == null) {
            return ResponseEntity.badRequest().build();
        } else {
            logger.info("add a member ...");
            return ResponseEntity.ok().build();
        }
    }

    @ApiOperation(value = "更新一个member", notes = "notes")
    @RequestMapping(value = "", method = RequestMethod.PATCH)
    public ResponseEntity updateMember(@ApiParam(name = "memberUid", value = "用户uid", required = true) @RequestParam(name = "memberUid", required = true) long memberUid,
                                       @ApiParam(name = "name", value = "用户name", required = false) @RequestParam(name = "memberName", required = false) String memberName,
                                       @ApiParam(name = "cleanname", value = "用户cleanname", required = false) @RequestParam(name = "memberCleanName", required = false) String memberCleanName) {
        // TODO
        return null;
    }

    @ApiOperation(value = "获取member分页数据", notes = "notes")
    @RequestMapping(value = "/getMemberByPageAndCondition", method = RequestMethod.GET)
    public List<MemberBean> getMemberPageAndCondition(@ApiParam(name = "start", value = "起始位置index", required = false, defaultValue = "0") @RequestParam(name = "start", required = false) int start,
                                                      @ApiParam(name = "limit", value = "要返回多少条", required = false, defaultValue = "10") @RequestParam(name = "limit", required = false) int limit,
                                                      @ApiParam(name = "name", value = "用户name", required = false) @RequestParam(name = "name", required = false) String name,
                                                      @ApiParam(name = "email", value = "用户email", required = false) @RequestParam(name = "email", required = false) String email,
                                                      @ApiParam(name = "role", value = "用户role", required = false, defaultValue = "0") @RequestParam(name = "role", required = false, defaultValue = "0") int role,
                                                      @ApiParam(name = "with_profiles", value = "with_profiles", required = false, defaultValue = "false") @RequestParam(name = "with_profiles", required = false, defaultValue = "false") boolean with_profiles,
                                                      @ApiParam(name = "bureau", value = "区域bureau1:1=BJ,2=SH,3=HK", required = false, defaultValue = "1") @RequestParam(name = "bureau", required = false, defaultValue = "1") int bureau) {
        int orderBy = MemberService.ORDERBY_UID_ASC;
        List<MemberBean> memberBeanList = memberService.getMemberBeanByPageAndCondition(start, limit, orderBy, name, email, role, with_profiles, bureau);
        return memberBeanList;
    }

}
